<template>
  <el-menu :default-active="activeMenu" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose" :collapse="isCollapse">
    <!-- logo -->
    <div class="logo" v-if="!isCollapse"><img src="../../assets/images/logo.png" alt="" style="margin-right: 5px" /> BIG CUTE</div>
    <div class="logo" v-else>
      <img src="../../assets/images/logo.png" alt="" />
    </div>
    <!-- 导航栏 -->
    <!-- 一级菜单栏 -->
    <el-menu-item @click="clickMenu(item)" v-for="item in noChildren" :index="item.path" :key="item.path">
      <i :class="'el-icon-' + item.icon"></i>
      <span slot="title">{{ item.label }}</span>
    </el-menu-item>
    <!-- 多级菜单栏 -->
    <el-submenu v-for="item in hasChildren" :index="item.label" :key="item.label">
      <template slot="title">
        <i :class="'el-icon-' + item.icon"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item @click="clickMenu(item2)" v-for="item2 in item.children" :index="item2.path" :key="item2.path">
          <i :class="'el-icon-' + item2.icon"></i>
          <span slot="title">{{ item2.label }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>

<script>
export default {
  data() {
    return {}
  },
  methods: {
    handleOpen(key, keyPath) {
      // console.log(key, keyPath)
    },
    handleClose(key, keyPath) {
      // console.log(key, keyPath)
    },
    // 点击菜单路由跳转
    clickMenu(item) {
      this.$router.push({ name: item.name })
      //添加tag标签
      this.$store.commit('selectTab', item)
    }
  },
  computed: {
    // 没有子菜单
    noChildren() {
      return this.menuList.filter(item => !item.children)
    },
    //有子菜单
    hasChildren() {
      return this.menuList.filter(item => item.children)
    },
    //获取菜单栏
    menuList() {
      return this.$store.state.menu.menu
    },
    //伸缩菜单栏
    isCollapse() {
      return this.$store.state.users.isCollapse
    },
    // 获取点击的菜单栏的path(激活菜单)
    activeMenu() {
      return this.$route.path
    }
  }
}
</script>

<style lang="less" scoped>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}
.el-menu {
  height: 100%;
  border: none;
  .is-active {
    color: #4060c7;
  }
  .logo {
    height: 48px;
    line-height: 48px;
    padding: 0 20px;
    list-style: none;
    position: relative;

    img {
      width: 25px;
      vertical-align: middle;
    }
  }
}
</style>
